package zzxkj.blog.utils;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import zzxkj.blog.dto.FirstPageBlog;
import zzxkj.blog.service.BlogService;
import zzxkj.blog.service.SearchService;

import java.util.List;

/**
 * @author zzxkj
 */
@Slf4j
@Component
public class EsUtil {
    private final BlogService blogService;
    private final SearchService searchService;

    public EsUtil(BlogService blogService, SearchService searchService) {
        this.blogService = blogService;
        this.searchService = searchService;
    }

    /**
     * 用于初始化es数据
     * 默认10000
     */
    public Long esInit(){
        int size = 10000;
        PageInfo page = new PageInfo();
        page.setPageSize(size);
        List<FirstPageBlog> blogs = blogService.getAllFirstPageBlog();
        long total = 0;
        total = searchService.initEsData(blogs);
        log.info("ES索引初始化成功，共{}条记录！", total);
        return total;
    }
}
